<?php


function list_komponen(){
	$periode_id=get_periode_gaji() ;
	$last_periode_id=get_last_periode_id($periode_id) ;
	$tipes = array(
		'1'=>'Tunjangan',
		'2'=>'Potongan',
		'3'=>'Iuran',
	);	

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nama Komponen'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'Tipe'=>array('style'=>'text-align:left;border-bottom:2px solid;width:5%'),  
		'Bulan Ini'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
		'Bulan Lalu'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
		'Selisih'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);

	$query = "SELECT komponen_gaji_id, nama_komponen_gaji, tipe 
			FROM pr_komponen_gaji
			ORDER BY tipe, komponen_gaji_id" ;
	
	$result = my_query($query); 
		
	while($ey = my_fetch_array($result)){
		$komponen_id=$ey['komponen_gaji_id'];
		$query = "SELECT SUM(kalkulasi) as kalkulasi 
			FROM pr_gaji_komponen  
			WHERE komponen_id={$komponen_id}
			AND periode_id=0" ;
		$res = my_fetch_array(my_query($query));
		$bulan_ini=$res['kalkulasi'];

		$query = "SELECT SUM(kalkulasi) as kalkulasi 
			FROM pr_gaji_komponen  
			WHERE komponen_id={$komponen_id}
			AND periode_id={$last_periode_id}" ;
		$res = my_fetch_array(my_query($query));
		$bulan_lalu=$res['kalkulasi'];	
	
		$detailproperty = array(
			'href'=>'index.php?com=pr_validasi&task=list_komponen_detail&komponen_id='.$ey['komponen_gaji_id'],
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		//if($bulan_ini<>0 OR $bulan_lalu<>0){ //TAMPILKAN YANG BUKAN 0
			$i++;
			$row[] = array(
				'no'=>position_text_align($i,'center'),  
				'gd'=>  $ey['nama_komponen_gaji'] ,
				'tipe'=>$tipes[$ey['tipe']],
				'bulan_ini'=>  position_text_align (rp_format($bulan_ini) ,'right'),      
				'bulan_lalu'=>  position_text_align (rp_format($bulan_lalu) ,'right'),     
				'selisih'=>   position_text_align (rp_format($bulan_ini-$bulan_lalu ) ,'right'),    
				'operasi'=> position_text_align( $detail_button, 'right'),
			); 
		//}									//TAMPILKAN YANG BUKAN 0
	}
 
	
	$datas = table_rows($row);
	//$paging = $kgPagerOBJ ->showPaging();	
	$view  = form_header( "..." , "..."  );
	$view .= form_field_display( '<span class="label_form"> '.get_tanggal_by_periode_id().'</span>'   , "<b>Periode Gaji</b>"    ); 
	$menuontop = array(  
		'per Karyawan' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_validasi&task=list_karyawan\''),
		'Excel Komponen' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_validasi&task=list_komponen_excel\''),
		);
	$printmenu = additional_menu_on_list( $menuontop , '');
	return  $view.$printmenu.table_builder($header , $datas ,  6 , false /*, $paging */  );
}

function list_pajak_karyawan($periode_id){ 
	
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:40%'),   
		'Pendapatan'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		'PTKP'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),   
		'PPh'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),   
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	
	$end=get_batas_tmt() ;
	if(isset($_GET['key'])){
		$query = "
			SELECT a.karyawan_id FROM karyawan a 
			INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
			INNER JOIN biodata c ON a.biodata_id = c.biodata_id 
			WHERE b.tmb  <= '{$end}' 
			AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			AND ( c.nama LIKE '%{$_GET['key']}%' OR a.nik ='{$_GET['key']}' )
		";
	}else{	
		$query = " SELECT a.karyawan_id FROM karyawan a 
			INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
			INNER JOIN biodata c ON a.biodata_id = c.biodata_id 
			WHERE b.tmb  <= '{$end}' 
			AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
		";	
	}
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=detail&karyawan_id='.$ey['karyawan_id'],
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$pajak = load_data_pajak($periode_id , $ey['karyawan_id'] ); 
		$row[] = array(
			'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
			'gd'=>  $karyawan['nama_gelar'] ,      
			'Pendapatan'=>  position_text_align( rp_format($pajak['pajak_07']), 'right'),      
			'PTKP'=>  position_text_align( rp_format($pajak['pajak_17']), 'right'),      
			'PPh1'=>  position_text_align( rp_format($pajak['pajak_23']), 'right') ,      
			'operasi'=> position_text_align( $detail_button, 'right')
		); 
	
	
	}
	$datas = table_rows($row);
	//$paging = $kgPagerOBJ ->showPaging();	
	$search = '<form method="GET">
		<input type="hidden" name="com" value="'.$_GET['com'].'" />
		<span style="font:10px verdana">
		<b>Pencarian cepat</b></span>
		<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
		<input class="simple_search" type="submit" value=" CARI " /> 
		</form>';
	$menuontop = array(    
		'Konfirmasi Gaji' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_validasi\''),
		'Excel' => array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel'.'\'')
	);
	$printmenu = additional_menu_on_list( $menuontop ,$search ); 
	$paging = $kgPagerOBJ ->showPaging();
	return  $view	.	$printmenu	.	table_builder($header , $datas ,  5 , false , $paging  );
}

function load_data_pajak($periode_id , $karyawan_id){
	$query = "SELECT * FROM pr_gaji_pajak WHERE periode_id ={$periode_id} AND karyawan_id = {$karyawan_id}"; 
	 
	$result = my_query($query); 
	if(my_num_rows($result) > 0){
		$row = my_fetch_array($result); 
		return $row;
	}
	return false;
}

function load_excel_pajak($periode_id){
	$end=get_batas_tmt() ;
	$header = array(
			'NIK'=>array(),
			'GAJI / PENSIUN ATAU THT / JHT'=>array(),
			'TUNJANGAN PPh'=>array(),
			'TUNJANGAN LAINNYA, UANG LEMBUR, DAN SEBAGAINYA'=>array(),
			'HONORARIUM DAN IMBALAN LAIN SEJENISNYA'=>array(),
			'PREMI ASURANSI YANG DIBAYAR PEMBERI KERJA'=>array(),
			'PENERIMAAN DALAM BENTUK NATURA DAN KENIKMATAN LAIN'=>array(),
			'JUMLAH (1 s.d. 6)'=>array(),
			'TANTIEM, BONUS, GRATIFIKASI, JASA PRODUKSI, DAN LAIN'=>array(),
			'JUMLAH PENGHASILAN BRUTO (7 + 8)'=>array(),
			'JUMLAH BIAYA JABATAN / BIAYA PENSIUN ATAS PENGHASILAN ANGKA 7'=>array(),
			'BIAYA JABATAN / BIAYA PENSIUN ATAS PENGHASILAN ANGKA 8'=>array(),
			'IURAN PENSIUN ATAU IURAN THT/ JHT'=>array(),
			'JUMLAH PENGURANGAN (10 + 11 + 12)'=>array(),
			'JUMLAH PENGHASILAN NETO (9 - 13)'=>array(),
			'PENGHASILAN NETO MASA SEBELUMNYA'=>array(),
			'JUMLAH PENGHASILAN NETO UNTUK PENGHITUNGAN PPh'=>array(),
			'PENGHASILAN TIDAK KENA PAJAK (PTKP)'=>array(),
			'PENGHASILAN KENA PAJAK SETAHUN / DISETAHUNKAN'=>array(),
			'PPh PASAL 21 ATAS PENGHASILAN KENA PAJAK SETAHUN'=>array(),
			'PPh PASAL 21 YANG TELAH DIPOTONG MASA SEBELUMNYA'=>array(),
			'PPh PASAL 21 TERUTANG'=>array(),
			'PPh PASAL 21 DAN PPh PASAL 26 YANG TELAH DIPOTONG'=>array(),
			'JUMLAH PPh PASAL 21'=>array()
	);
	$query = " SELECT a.nik , a.karyawan_id FROM karyawan a 
			INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
			INNER JOIN biodata c ON a.biodata_id = c.biodata_id 
			WHERE b.tmb  <= '{$end}' 
			AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
		";	
	$result = my_query($query);
	$row = array();
	while($ey = my_fetch_array($result)){
		$pajak = load_data_pajak($periode_id , $ey['karyawan_id'] ); 
		$row[]=array(
			'a'=>'\'' . $ey['nik'],
			'1'=>$pajak[ 'pajak_01'],
			'2'=>$pajak[ 'pajak_02'],
			'3'=>$pajak[ 'pajak_03'],
			'4'=>$pajak[ 'pajak_04'],
			'5'=>$pajak[ 'pajak_05'],
			
			'6'=>$pajak[ 'pajak_06'],
			'7'=>$pajak[ 'pajak_07'],
			'8'=>$pajak[ 'pajak_08'],
			'9'=>$pajak[ 'pajak_09'],
			'10'=>$pajak[ 'pajak_10'],
			'11'=>$pajak[ 'pajak_11'],
			'12'=>$pajak[ 'pajak_12'],
			'13'=>$pajak[ 'pajak_13'],
			'14'=>$pajak[ 'pajak_14'],
			'15'=>$pajak[ 'pajak_15'],
			
			'16'=>$pajak[ 'pajak_16'],
			'17'=>$pajak[ 'pajak_17'],
			'18'=>$pajak[ 'pajak_18'],
			'19'=>$pajak[ 'pajak_19'],
			'20'=>$pajak[ 'pajak_20'],
			
			'21'=>$pajak[ 'pajak_21'],
			'22'=>$pajak[ 'pajak_22'],
			'23'=>$pajak[ 'pajak_23'],
		
		
		); 
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,  26 ,false );
}


function list_validasi_pajak_detail($karyawan_id){
	$karyawan = loaddata_karyawan($karyawan_id);
	$query = " SELECT * FROM pr_gaji_pajak 
		WHERE periode_id=0
		AND karyawan_id=".$karyawan_id
	;
	$result = my_query($query);	
	$pajaks = my_fetch_array($result);	
	
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nama Komponen Pajak'=>array('style'=>'text-align:left;border-bottom:2px solid;width:65%'),  
		'Nominal (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	
	$query = "SELECT * FROM pr_komponen_pajak
			ORDER BY komponen_pajak_id"
	;
	$result = my_query($query);
	$row = array();
	$i=0;

	while($ey = my_fetch_array($result)){
		$i++;
		$detailproperty = array(
			'href'=>'index.php?com=pr_adjustment&task=rinci&karyawan_id='.$karyawan_id.
				'&komponen_id='.$ey['komponen_pajak_id'],
			'title'=>'Detail Rinci Pajak'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$nominal=$pajaks['pajak_'.sprintf('%02d' ,$i)];
		$row[] = array(
			'e'=>position_text_align($i,'center'),
			'nama'=>$ey['label_pajak'] ,
			'nom'=> position_text_align(rp_format($nominal), 'right' ),
			'op'=> position_text_align($detail_button , 'right' ),
		);
	}
	
	$datas = table_rows($row); 
	$pro =   karyawan_header_detail($karyawan,true  );
	
	$menuontop = array(  
		'Back' => array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'\'')
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	return  $pro.$printmenu.table_builder($header , $datas ,  6 , false   );
}